
.MODEL FLAT
.CODE

EXTRN _CreateDreamInst:PROC

_push_reg@8 PROC
	mov eax, dword ptr [esp]
	mov ecx, dword ptr [esp+4]
	mov edx, dword ptr [esp+8]
	sub esp, 4*7
	mov dword ptr [esp+4*0], eax	; eip
	mov dword ptr [esp+4*1], ebx	; ebx
	mov dword ptr [esp+4*2], esi	; esi
	mov dword ptr [esp+4*3], edi	; edi
	mov dword ptr [esp+4*4], ebp	; ebp
	mov dword ptr [esp+4*5], esp	; esp
	add dword ptr [esp+4*5], (7+3)*4
	mov eax, esp
	push edx
	push eax
	call ecx
	add esp, 4*7
	ret 8
_push_reg@8 ENDP

_save_context@4 PROC
	; fill rax
	mov ecx, dword ptr [esp]
	mov eax, dword ptr [esp+4]
	mov dword ptr [eax+4*0], ecx	; eip
	mov dword ptr [eax+4*1], ebx	; ebx
	mov dword ptr [eax+4*2], esi	; esi
	mov dword ptr [eax+4*3], edi	; edi
	mov dword ptr [eax+4*4], ebp	; ebp
	mov dword ptr [eax+4*5], esp	; esp
	add dword ptr [eax+4*5], 8
	ret 4
_save_context@4 ENDP


_pop_reg@8 PROC
	mov ecx, dword ptr [esp+4]
	mov eax, dword ptr [esp+8]
	;add esp, 4
	;pop ecx
	;pop eax

	;change stack frame
	mov ebx, dword ptr[ecx+4*1]
	mov esi, dword ptr[ecx+4*2]
	mov edi, dword ptr[ecx+4*3]
	mov ebp, dword ptr[ecx+4*4]
	mov esp, dword ptr[ecx+4*5]
	push dword ptr[ecx+4*0]
	ret
_pop_reg@8 ENDP

;extern "C" void __stdcall run_with_stack(void* stack_start:0, lolix::size_type stack_size:4, void (__stdcall*:8)(void*), void* usrptr:12);
_run_with_stack@16 PROC
	;push ecx
	mov eax, dword ptr [esp + 4 + 0]	; stack_start
	add eax, dword ptr [esp + 4 + 4]	; stack_size
	sub eax, 16	+ 12;	proc, usrptr, old_ebp, old_esp, [end] -> align to 16 bytes
	mov ecx, dword ptr [esp + 4 + 8]	; proc
	mov dword ptr [eax], ecx
	mov ecx, dword ptr [esp + 4 + 12]	; usrptr
	mov dword ptr [eax + 4], ecx
	mov dword ptr [eax + 8], ebp
	mov dword ptr [eax + 12], esp
	mov esp, eax
	mov ebp, eax
	pop eax
	call eax
	mov ebp, [esp]
	mov esp, [esp+4]
	;pop ecx
	ret 16
_run_with_stack@16 ENDP


_get_stack@0 PROC
	mov eax, esp
	add eax, 4
	ret
_get_stack@0 ENDP

;lolix::LOLIX_RETNV
;lxsamsara::_inst::recurrence_inst::dream(lxsamsara::dream** out_ptr)const
;8->[out_ptr]
;4->[this]
;0->[eip]
?nap@recurrence_inst@_inst@lxsamsara@@UBG?AW4LOLIX_RETNV@lolix@@PAPAVdream@3@@Z PROC
	sub		esp, 24		;_RegBack$Size
	push	esp
	call	_save_context@4
	mov		eax, dword ptr [esp + 24]
	mov		dword ptr [esp], eax	;	eip
	lea		eax, [esp + 24 + 12]	;	std call this + out_ptr + eip.
	mov		dword ptr [esp+20], eax	;	old esp
	push	esp
	push	dword ptr [esp + 32]
	push	dword ptr [esp + 40]
	call	_CreateDreamInst
	add		esp, 12
	ret		8 + 24
?nap@recurrence_inst@_inst@lxsamsara@@UBG?AW4LOLIX_RETNV@lolix@@PAPAVdream@3@@Z ENDP

END
